Yes.
try
and catch
WorkHere is how
try
andcatch
work:
- When an
Exception
is thrown by a statement in thetry{}
block, thecatch{}
blocks are examined one-by-one starting starting with the first.- The first
catch{}
block to match the type of theException
gets control.
- In the diagram, X, Y, and Z represent different types of exceptions.
- For example, Y might be
IOException
.- Only one
catch{}
block gets control.- If no
catch{}
block matches theException
, none is picked, and execution leaves this method (just as if there were notry{}
block.)- The most specific
Exception
types should appear first in the structure, followed by the more generalException
types.- The statements in the chosen
catch{}
block execute sequentially. After the last statement executes, control goes to the first statement that follows thetry/catch
structure.- Control does not return to the
try
block.
Must the catch{}
blocks list all possible Exception
s?